using System;
using System.Collections.Generic;
using System.Text;

namespace Pricom.Controls
{
  class ListBox : System.Windows.Forms.ListBox
  {
    public void Add(string s)
    {
      if (InvokeRequired)
        BeginInvoke(new System.Windows.Forms.MethodInvoker(delegate() { Add(s); }));
      else
      {
        if (!IsHandleCreated)
          Cache(s);
        else
        {
          FlushCache();
          Items.Add(s);
        }
      }
    }

    public void Clear()
    {
      if (InvokeRequired)
        BeginInvoke(new System.Windows.Forms.MethodInvoker(delegate() { Clear(); }));
      else
      {
        cache.Clear();
        Items.Clear();
      }
    }

    protected override void OnMouseClick(System.Windows.Forms.MouseEventArgs e)
    {
      {
        //int i = IndexFromPoint(new System.Drawing.Point(e.X, e.Y));
        //string item = (i == -1 ? "" : Items[i].ToString());
        //Scripting.Script s = new Scripting.Script();
        //s.Test();
        //s.RunScript("OnValamiClicked", new object[] {item});
      }
      base.OnMouseClick(e);
    }

    protected override void OnHandleCreated(EventArgs e)
    {
      base.OnHandleCreated(e);
      FlushCache();
    }

    private void FlushCache()
    {
      foreach (string cached in cache)
        base.Items.Add(cached);
      cache.Clear();
    }

    private void Cache(string s)
    {
      cache.Add(s);
    }

    private List<string> cache = new List<string>();
  }
}
